package com.qcby.mycommunity_003.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.mycommunity_003.annotation.LogAnnotation;
import com.qcby.mycommunity_003.entity.Role;
import com.qcby.mycommunity_003.entity.RoleMenu;
import com.qcby.mycommunity_003.form.RoleForm;
import com.qcby.mycommunity_003.mapper.RoleMapper;
import com.qcby.mycommunity_003.service.RoleMenuService;
import com.qcby.mycommunity_003.service.RoleService;
import com.qcby.mycommunity_003.util.Result;
import com.qcby.mycommunity_003.vo.PageVo;
import com.qcby.mycommunity_003.vo.RoleListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @GetMapping("/list")
    public Result Rolelist(RoleListVo listVo){
        PageVo pageVo=roleService.Rolelist(listVo);
        return Result.ok().put("data",pageVo);
    }

    /**
     * 菜单列表
     * @return
     */
    @GetMapping("/getRoleList")
    public Result getRoleList(){
        List<Role> list = roleService.list();
        return Result.ok().put("data",list);
    }

    /**
     * 添加角色，需要设置创建时间
     * 在角色id表，也要添加权限
     * @param roleForm
     * @return
     */
    @LogAnnotation("添加用户")
    @PostMapping("/add")
    public Result addRole(@RequestBody RoleForm roleForm){
        Role role=new Role();
        role.setRoleName(roleForm.getRoleName());
        role.setRemark(roleForm.getRemark());
        role.setType(roleForm.getType());
        role.setCreateTime(new Date());
        //插入后，id会返回吗？？ 测试过了，会的。
        roleService.save(role);
        Integer roleId = role.getRoleId();
        List<Integer> MenuIds = roleForm.getMenuIdList();
        for (Integer menuId : MenuIds) {
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenuService.save(roleMenu);
        }
        return Result.ok();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result SearchById(@PathVariable Integer id){
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(id!=null,"role_id",id);
        Role one = roleService.getOne(queryWrapper);
        HashMap<String,Object> data=new HashMap<>();
        data.put("roleId",one.getRoleId());
        data.put("roleName",one.getRoleName());
        data.put("type",one.getType());
        data.put("remark",one.getRemark());
        data.put("createTime",one.getCreateTime());
        QueryWrapper<RoleMenu> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("role_id",one.getRoleId());
        List<RoleMenu> list = roleMenuService.list(queryWrapper1);
        List<Integer> ids=new ArrayList<>();
        for (RoleMenu roleMenu : list) {
            ids.add(roleMenu.getMenuId());
        }
        data.put("menuIdList",ids);
        return Result.ok().put("data",data);
    }

    /**
     * 修改
     * @param roleForm
     * @return
     */
    @LogAnnotation("修改用户信息")
    @PutMapping("/edit")
    public Result update(@RequestBody RoleForm roleForm){
    Role role=new Role();
    BeanUtils.copyProperties(roleForm,role);
    //方案一：把所有的当前用户id名下的菜单删除，再根据传过来的菜单列表插入
    //方案二：把当前用户的所有菜单id找到list1，和传的list2对比。
        //1有2没有，删除；1没有2有，插入。1,2都有不用管
   QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
   queryWrapper.eq("role_id",roleForm.getRoleId());
   roleMenuService.remove(queryWrapper);
   List<Integer> list2 = roleForm.getMenuIdList();
    for (Integer i : list2) {
            RoleMenu rm=new RoleMenu();
            rm.setRoleId(roleForm.getRoleId());
            rm.setMenuId(i);
            roleMenuService.save(rm);
        }
        Role role1 = roleService.getById(role.getRoleId());
        role.setCreateTime(role1.getCreateTime());
        roleService.updateById(role);
        return Result.ok();
    }
    @Autowired
   private RoleMapper roleMapper;
    @LogAnnotation("删除用户信息")
    @DeleteMapping("/del")
    public Result del(@RequestBody List<Integer> ids){
        //1.删除角色菜单表
        QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.in(ids!=null&&ids.size()>0,"role_id",ids);
        roleMenuService.remove(queryWrapper);
//        if(!remove){return Result.error("角色菜单表删除失败");}
        //2.删除角色表
        roleMapper.deleteBatchIds(ids);
        return Result.ok();
    }
}

